package com.appkubes.fintech.admin.application.repair.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.appkubes.fintech.admin.application.repair.dao.RepairChannelRepository;
import com.appkubes.fintech.admin.application.repair.dao.RepairOrderInfoRepository;
import com.appkubes.fintech.admin.application.repair.request.OrderInfoRequest;
import com.appkubes.fintech.admin.application.repair.request.OrderItemRequest;
import com.appkubes.fintech.admin.application.repair.request.RepairOrderInfoRequest;
import com.appkubes.fintech.admin.application.repair.response.OrderInfoResponse;
import com.appkubes.fintech.admin.application.repair.response.RepairOrderInfoResponse;
import com.appkubes.fintech.admin.application.repair.response.RepairOrderItemResponse;
import com.appkubes.fintech.admin.application.repair.service.RepairOrderInfoService;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.common.util.ConversionUtil;
import com.appkubes.fintech.core.po.*;
import com.appkubes.fintech.core.pojo.FintechRepairOrderItemMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/*
 * 维修订单
 * @author    xqc
 * @date    2020/02/05 10:51:16
 */
@Service
@Slf4j
public class RepairOrderInfoServiceImpl implements RepairOrderInfoService {


    @Resource
    private RepairOrderInfoRepository repairOrderInfoRepository;

    @Resource
    private RepairChannelRepository channelRepository;
    @Resource
    private FintechRepairOrderItemMapper itemMapper;
    @Override
    public PageInfo<RepairOrderInfoResponse> queryPage(RepairOrderInfoRequest request) {
        PageHelper.startPage(request.getPage(), request.getPageSize());
        FintechRepairOrderInfoExample example=   new FintechRepairOrderInfoExample();
        example.setOrderByClause(" create_time desc");

        FintechRepairOrderInfoExample.Criteria criteria=example.createCriteria();
//        if(StringUtils.isNotBlank(vo.getPhone())){
//            criteria.andPhoneEqualTo(vo.getPhone());
//        }

        if(StringUtils.isNotBlank(request.getDeviceInfo())){
            criteria.andDeviceInfoLike("%"+ request.getDeviceInfo()+"%");
        }
        if(request.getStatus()!=null){
            criteria.andStatusEqualTo(request.getStatus());
        }
        if(StringUtils.isNotBlank(request.getUserName())){
            criteria.andUserNameLike("%"+request.getUserName()+"%");
        }
        if(StringUtils.isNotBlank(request.getOrderNo())){
            criteria.andOrderNoLike("%"+request.getOrderNo()+"%");
        }

        if(StringUtils.isNotBlank(request.getOrderNo())){
            criteria.andOrderNoLike("%"+request.getOrderNo()+"%");
        }
        if(request.getCreateTime2()!=null){
            criteria.andCreateTimeLessThan(request.getCreateTime2());
        }
        if(request.getCreateTime1()!=null){
            criteria.andCreateTimeGreaterThan(request.getCreateTime1());
        }
        List<FintechRepairOrderInfo> repairOrderInfoList = repairOrderInfoRepository.selectByExample(example);

        List<RepairOrderInfoResponse> responseList = ConversionUtil.ofList(repairOrderInfoList, RepairOrderInfoResponse.class);
        return new PageInfo<>(responseList);
    }


    @Override
     public Response<OrderInfoResponse> saveOrder(OrderInfoRequest request, FintechUserBaseInfo baseInfo){
        FintechRepairOrderInfo orderInfo=new  FintechRepairOrderInfo();
        BeanUtil.copyProperties(request,orderInfo);
        orderInfo.setOrderNo(makeOrderNo());
        orderInfo.setCreateTime(new Date());
        orderInfo.setUserName(baseInfo.getRealName());
        orderInfo.setId(UUID.randomUUID().toString().replace("-",""));
        orderInfo.setCreateTime(new Date());
        orderInfo.setUpdateTime(new Date());
        orderInfo.setStatus(0);

        BigDecimal totalAmt=new BigDecimal(0);
        for(int i=0;i<request.getItemList().size();i++){
            OrderItemRequest itemRequest=request.getItemList().get(i);
            FintechRepairChannelInfoExample example=new FintechRepairChannelInfoExample();
            example.createCriteria().andCodeEqualTo(itemRequest.getItemCode());
            FintechRepairChannelInfo channelInfo=channelRepository.selectOneByExample(example);
            FintechRepairOrderItem item1=new FintechRepairOrderItem();
            item1.setCreateTime(new Date());
            item1.setId(UUID.randomUUID().toString().replace("-",""));
            item1.setItemCode(channelInfo.getCode());
            item1.setItemName(channelInfo.getName());
            item1.setSeqNo(i+1);
            item1.setUpdateTime(new Date());
            item1.setUserId(baseInfo.getUserKey());
            item1.setOrderId(orderInfo.getId());
            item1.setPrice(channelInfo.getPrice());
            totalAmt.add(item1.getPrice());
            itemMapper.insert(item1);
        };
        orderInfo.setPrePrice(totalAmt);
        repairOrderInfoRepository.insert(orderInfo);
        OrderInfoResponse orderInfoResponse=new OrderInfoResponse();
        orderInfoResponse.setOrderId(orderInfo.getId());
        orderInfoResponse.setOrderNo(orderInfo.getOrderNo());
        return Response.success(orderInfoResponse);
    }


    @Override
    public Response update(RepairOrderInfoRequest repairOrderInfoRequest) {
        if (StringUtils.isBlank(repairOrderInfoRequest.getId())) {
            return Response.fail("主键不能为空");
        }
        FintechRepairOrderInfo old = repairOrderInfoRepository.selectByPrimaryKey(repairOrderInfoRequest.getId());
        if (old == null) {
            return Response.fail("对象不存在");
        }
        BeanUtils.copyProperties(repairOrderInfoRequest, old, "id", "createTime", "updateTime");
        repairOrderInfoRepository.updateByPrimaryKey(old);
        return Response.success();
    }


    @Override
    public Response deleteRepairOrderInfo(String id) {
        if (StringUtils.isBlank(id)) {
            return Response.fail("主键不能为空");
        }
        FintechRepairOrderInfo repairOrderInfo = repairOrderInfoRepository.selectByPrimaryKey(id);
        if (repairOrderInfo == null) {
            return Response.fail("对象不存在");
        }
        repairOrderInfoRepository.deleteByPrimaryKey(repairOrderInfo.getId());
        return Response.success();
    }

    @Override
    public Response<RepairOrderInfoResponse> getById(String id) {
        FintechRepairOrderInfo repairOrderInfo = repairOrderInfoRepository.selectByPrimaryKey(id);
        RepairOrderInfoResponse repairOrderInfoResponse = new RepairOrderInfoResponse();
        BeanUtils.copyProperties(repairOrderInfo, repairOrderInfoResponse);
        FintechRepairOrderItemExample example=new FintechRepairOrderItemExample();
        example.setOrderByClause("seq_no");
        example.createCriteria().andOrderIdEqualTo(id);
        List<FintechRepairOrderItem> itemList=itemMapper.selectByExample(example);
        List<RepairOrderItemResponse> itemResponseList=ConversionUtil.ofList(itemList,RepairOrderItemResponse.class);
        repairOrderInfoResponse.setItemList(itemResponseList);
        return Response.success(repairOrderInfoResponse);

    }

    @Override
    public void deleteById(String id) {
        repairOrderInfoRepository.deleteByPrimaryKey(id);
    }
    @Override
    public Response approval(RepairOrderInfoRequest request){

        FintechRepairOrderInfo orderInfo=repairOrderInfoRepository.selectByPrimaryKey(request.getId());
        orderInfo.setApprovalMsg(request.getApprovalMsg());
        orderInfo.setPeoplePrice(request.getPeoplePrice());
        orderInfo.setStatus(request.getStatus());
        orderInfo.setUpdateTime(new Date());
        repairOrderInfoRepository.updateByPrimaryKey(orderInfo);
        return Response.success(orderInfo);
    }

    private String makeOrderNo(){
        String fix= "RI"+DateUtil.format(new Date(),"yyyyMMddHHmm");
        String maxCode=repairOrderInfoRepository.maxCode(fix+"%");
        if(StringUtils.isBlank(maxCode)){
            return fix+"001";
        }
        else{
            return com.appkubes.fintech.common.util.StringUtils.autoGrowth(maxCode);
        }
    }

}
